<?php
class ObservationController extends Controller{
        /**
         * Filters for this controller
         * 
         * @return type
         */
        public function filters()
        {
            return array( 'accessControl' ); // perform access control for CRUD operations
        }

        /**
         * Access rules that will be used by the accessControl-filter
         * 
         * @return type
         */
        public function accessRules()
        {
            return array(
                array('allow', // allow authenticated users to access all actions
                    'users'=>array('@'),
                    'actions'=>array('index','add'),
                ),
                array('deny'),
            );
        }
        /**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/observation/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}
        
        /**
         * This is the action for adding an observation for current logged
         * in user.
         */
        public function actionAdd()
        {
            // New model
            $model = new Observation;
            
            // User Model filled with data of current logged in user. For getting all telescopes
            $model->telescope = CHtml::listData(
                    User::model()->findByPk(
                            Yii::app()->user->id // ->id calls getId() on the UserIdentity class
                    )->telescopes, 'telescope_ID', 'name');
            
            // if it is ajax validation request
            if(isset($_POST['ajax']) && $_POST['ajax']==='add-form')
            {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            // collect user input data
            if(isset($_POST['Observation']))
            {
                $model->attributes=$_POST['Observation'];
                
                if(!$model->validate())
                {
                    // ViewModel didn't pass validation, return to add-view while displaying errors
                    $this->render('add',array('model'=>$model));
                    Yii::app()->end();
                }

                $model->grouptotal = $model->groupnorth + $model->groupsouth;
                $model->spottotal = $model->spotnorth + $model->spotsouth;
                $model->wolff = 10*$model->grouptotal+$model->spottotal;
                $model->correction = 0;
                $model->telescope_ID = $model->telescope;

                // Give the new observation the user_ID of the currently logged in user.
                $model->user_ID = Yii::app()->user->id;

                // save() by default validates the model
                if($model->save(/*$runValidation=true*/))
                {
                        $uploadedImage = CUploadedFile::getInstance($model,'image');
                        if($uploadedImage instanceof CUploadedFile)
                        {
                                // An image is uploaded
                                $new_image = new Image;
                                $new_image->image_name = $uploadedImage;
                                $new_image->observation_ID = $model->observation_ID;
                                if($new_image->save())
                                {                                    
                                    $new_image->image_name->saveAs(
                                            Yii::app()->basePath.
                                            Yii::app()->params['UPLOAD_DIR'].
                                            DIRECTORY_SEPARATOR.
                                            Yii::app()->user->id.
                                            '_'.
                                            $model->observation_ID.
                                            '_'.
                                            $new_image->image_ID.
                                            '_'.
                                            $uploadedImage->getName()/*, $deleteTempFile=true*/);                                        
                                }
                        }

                        // Redirect user back to originating page
                        $this->redirect(array('overview/index'));
                }                
            }

            // display the form for adding an observation
            $this->render('add',array('model'=>$model));
        }
}
?>
